package com.sh.data.engine.job.admin.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.data.engine.job.admin.core.model.XxlJobGroup;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by xuxueli on 16/9/30.
 */
@Mapper
public interface XxlJobGroupDao extends BaseMapper<XxlJobGroup> {

    default List<XxlJobGroup> findAll() {
        return this.selectList(null);
    }

    default List<XxlJobGroup> findByAddressType(int addressType) {
        LambdaQueryWrapper<XxlJobGroup> wrapper =
            new QueryWrapper<XxlJobGroup>()
                .lambda()
                .eq(XxlJobGroup::getAddressType, addressType)
                .orderByAsc(XxlJobGroup::getAppname, XxlJobGroup::getTitle, XxlJobGroup::getId);
        return this.selectList(wrapper);
    }

    default int save(XxlJobGroup xxlJobGroup) {
        return this.insert(xxlJobGroup);
    }

    default int update(XxlJobGroup xxlJobGroup) {
        return this.updateById(xxlJobGroup);
    }

    default int remove(int id) {
        return this.deleteById(id);
    }

    default XxlJobGroup load(int id) {
        return this.selectById(id);
    }

    default List<XxlJobGroup> pageList(int offset, int pagesize, String appname, String title) {
        LambdaQueryWrapper<XxlJobGroup> wrapper = new QueryWrapper<XxlJobGroup>().lambda();
        if (StringUtils.isNotBlank(appname)) {
            wrapper.like(XxlJobGroup::getAppname, appname);
        }
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(XxlJobGroup::getTitle, title);
        }
        wrapper.orderByAsc(XxlJobGroup::getAppname, XxlJobGroup::getTitle, XxlJobGroup::getId);
        Page<XxlJobGroup> xxlJobGroupPage =
            this.selectPage(new Page<XxlJobGroup>(offset, pagesize), wrapper);
        return xxlJobGroupPage.getRecords();
    }

    default int pageListCount(int offset, int pagesize, String appname, String title) {
        LambdaQueryWrapper<XxlJobGroup> wrapper = new QueryWrapper<XxlJobGroup>().lambda();
        if (StringUtils.isNotBlank(appname)) {
            wrapper.like(XxlJobGroup::getAppname, appname);
        }
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(XxlJobGroup::getTitle, title);
        }
        return Math.toIntExact(this.selectCount(wrapper));
    }
}
